package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.domain.WebConfig;
import com.demo.model.R;
import com.demo.service.WebConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/webConfigs")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;

    @GetMapping
    public R<Page<WebConfig>> findByPage(Page<WebConfig> page,String name,String type){
        return  R.ok(webConfigService.findByPage(page,name,type));
    }

    @PostMapping
    public R add(@RequestBody @Validated WebConfig webConfig){
        boolean save = webConfigService.save(webConfig);
        if(save){
            return R.ok("新增成功");
        }

        return R.ok("新增失败");
    }

    @PatchMapping
    public R update(@RequestBody @Validated WebConfig webConfig){
        boolean update = webConfigService.updateById(webConfig);
        if(update){
            return R.ok("更新成功");
        }

        return R.ok("更新失败");
    }

    @PostMapping("/delete")
    public R delete(@RequestBody @Validated String[] ids){
        if(null == ids || ids.length == 0){
            R.fail("删除时需要给出数据id");
        }
        boolean remove = webConfigService.removeByIds(Arrays.asList(ids));
        if(remove){
            return R.ok("删除成功");
        }

        return R.ok("删除失败");
    }

}
